Substituting using a method
This function operates in just the same way as substFirst.
However, this function has been implemented as a method and added to the string
class.
Instead of calling a function such as
myFunction("foo"),
a method looks like a property with arguments, e.g.,
"foo".myFunction().
The procedure to add a method to the String class
will change in the next version
of Netscape Navigator (code-named "Atlas"). Instead of adding methods
to the empty string, you will need to add the methods to the class
String.
Warning: Adding string methods is *broken* in Atlas PR1, although
it should work with Atlas PR2 and later.
// Same as substFirst but as a string method
function subst1(c1, c2)
{
if (this == "") return this
// find the character string
var i = this.indexOf(c1)
// return original string if not found
if (i < 0) return this
// extract the characters before and after the string
var s1 = (""+this).substring(0, i)
var s2 = (""+this).substring(i+1, this.length)
return s1+c2+s2
}
// Add to the string class
String.subst1 = subst1
// For early versions of navigator
"".subst1 = subst1
Copyright ©1998 by Charles River Media, All Rights Reserved